import {checkMember, memberLogin, msmsLogin} from '@/api/common'
import { myRule } from '@/api/system'

const state = {
    account: localStorage.account || '',
    token: localStorage.token || '',
    realname: localStorage.realname || '',
    avatar: localStorage.avatar || '',
    userStatus: localStorage.userStatus || '',
    userId: localStorage.userId || '',
    accept: localStorage.accept || '',
    auth: localStorage.auth || '',
    permissions: []
}

const getters = {
    isLogin: state => {
        let retn = false
        if (state.token) {
            retn = true
        }
        return retn
    }
}

const actions = {
    login({commit}, data) {
        return new Promise((resolve, reject) => {
            memberLogin(data).then(res => {
                commit('setUserData', res.data)
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    checkMember({commit}, data) {
        return new Promise((resolve, reject) => {
            checkMember(data).then(res => {
                commit('setUserData', res.data)
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    smsLogin({commit}, data) {
        return new Promise((resolve, reject) => {
            msmsLogin(data).then(res => {
                commit('setUserData', res.data)
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    logout({commit}) {
        commit('removeUserData')
        commit('reSetPermissions')
        commit('menu/invalidRoutes', null, {root: true})
    },
    // 获取我的权限
    getPermissions({state, commit}) {
        return new Promise((resolve, reject) => {
            myRule().then(res => {
                const rule_list = res.data.rule_list;
                let data = [];
                rule_list.forEach(i => {
                    if (i.front_path) data.push(i.front_path)
                });
                commit('setPermissions', data)
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    }
}

const mutations = {
    setUserData(state, data) {
        localStorage.setItem('account', data.phone)
        localStorage.setItem('token', data.token)
        localStorage.setItem('realname', data.realname)
        localStorage.setItem('avatar', data.face)
        localStorage.setItem('userStatus', data.status)
        localStorage.setItem('userId', data.id)
        localStorage.setItem('accept', data.accept)
        localStorage.setItem('auth', data.auth)
        state.account = data.account
        state.token = data.token
        state.realname = data.realname
        state.avatar = data.face
        state.userStatus = data.userStatus
        state.userId = data.userId
        state.accept = data.accept
        state.auth = data.auth
    },
    removeUserData(state) {
        localStorage.removeItem('account')
        localStorage.removeItem('token')
        localStorage.removeItem('realname')
        localStorage.removeItem('avatar')
        localStorage.removeItem('userStatus')
        localStorage.removeItem('userId')
        localStorage.removeItem('accept')
        localStorage.removeItem('auth')
        state.account = ''
        state.token = ''
        state.realname = ''
        state.avatar = ''
        state.userStatus = ''
        state.userId = ''
        state.accept = ''
        state.auth = ''
    },
    setPermissions(state, data) {
        localStorage.setItem('permissions', data)
        state.permissions = data
    },
    reSetPermissions(state, data) {
        localStorage.setItem('permissions', [])
        state.permissions = []
    }
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
